/**
 * 
 */
package org.t2.game.server.module.recharge;

import org.gaming.fakecmd.annotation.InternalCmd;
import org.gaming.fakecmd.annotation.PlayerCmd;
import org.gaming.ruler.eventbus.EventBus;
import org.gaming.tool.DateTimeUtil;
import org.gaming.tool.LocalDateTimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.t2.design.module.RechargeTable;
import org.t2.design.module.autogen.RechargeAbstractTable.RechargeCfg;
import org.t2.game.server.foundation.player.PlayerContext;
import org.t2.game.server.foundation.player.PlayerContextManager;
import org.t2.game.server.module.difchannel.DifChannelManager;
import org.t2.game.server.module.marquee.service.MarqueeService;
import org.t2.game.server.module.player.model.Player;
import org.t2.game.server.module.player.service.PlayerService;
import org.t2.game.server.module.recharge.event.RechargeEvent;
import org.t2.game.server.module.recharge.message.RechargeInternalMessage;
import org.t2.game.server.module.recharge.model.RechargeOrder;
import org.t2.game.server.module.recharge.model.RechargePlayer;
import org.t2.game.server.module.recharge.service.RechargeOrderService;
import org.t2.game.server.module.recharge.service.RechargePlayerService;
import org.t2.game.server.module.tips.GameCause;

import pb.t2.protobuf.recharge.Recharge.RechargeMarkFinishRequest;
import pb.t2.protobuf.recharge.Recharge.RechargeMarkFinishResponse;
import pb.t2.protobuf.recharge.Recharge.RechargeOrderRequest;
import pb.t2.protobuf.recharge.Recharge.RechargeOrderResponse;
import pb.t2.protobuf.recharge.Recharge.RechargeSuccessMessage;
import pb.t2.protobuf.recharge.Recharge.RechargeUnFinishRequest;
import pb.t2.protobuf.recharge.Recharge.RechargeUnFinishResponse;

/**
 * 
 */
@Controller
public class RechargeHandler {

	private static Logger logger = LoggerFactory.getLogger(RechargeHandler.class);
	
	@Autowired
	private RechargeOrderService rechargeOrderService;
	@Autowired
	private DifChannelManager difChannelManager;
	@Autowired
	private PlayerContextManager playerContextManager;
	@Autowired
	private PlayerService playerService;
	@Autowired
	private RechargePlayerService rechargePlayerService;
	@Autowired
	private MarqueeService marqueeService;

	@PlayerCmd
	public RechargeOrderResponse order(PlayerContext playerContext, RechargeOrderRequest request) {
		rechargeOrderService.checkOrdering(playerContext.getId(), request.getProductId());

		RechargeOrder rechargeOrder = rechargeOrderService.createOrder(playerContext.getId(), request.getProductId(),
				false, GameCause.RECHARGE);

		RechargeOrderResponse.Builder response = RechargeOrderResponse.newBuilder();
		response.setOrderId(rechargeOrder.getId());
		response.setProductId(rechargeOrder.getProductId());
		response.setExtraInfo(rechargeOrder.getExtraInfo());
		difChannelManager.onRecharge(rechargeOrder);
		return response.build();
	}

	@InternalCmd
	public void handle(RechargeInternalMessage message) {
		RechargeOrder rechargeOrder = message.getRechargeOrder();

		Player player = playerService.getEntity(rechargeOrder.getPlayerId());
		boolean createPay = false;
		if (!rechargeOrder.isVirtual() && LocalDateTimeUtil.isSameDay(player.getInsertTime(), LocalDateTimeUtil.now())) {
			// 创号当天充值
			player.setCreatePay(true);
			createPay = true;
		}
		boolean firstPay = false;
		if (player.getFirstPayTime() == 0) {
			// 首次付费时间
			player.setFirstPayTime(DateTimeUtil.currMillis());
			firstPay = true;
		}
		// 记录充值
		player.setRecharge(player.getRecharge() + rechargeOrder.getPayMoney());
		playerService.update(player);
		
		RechargePlayer entity = rechargePlayerService.getEntity(rechargeOrder.getPlayerId());
		entity.getUnMarkOrders().add(rechargeOrder.getId());
		rechargePlayerService.update(entity);

		EventBus.post(RechargeEvent.of(player, rechargeOrder.getPayMoney(), createPay, firstPay));

		RechargeSuccessMessage.Builder pbMessage = RechargeSuccessMessage.newBuilder();
		pbMessage.setRechargeId(rechargeOrder.getId());
		playerContextManager.push(rechargeOrder.getPlayerId(), RechargeSuccessMessage.Proto.ID, pbMessage.build());
		logger.info("充值订单{}发货已通知前端", rechargeOrder.getId());
		
		RechargeCfg rechargeCfg = RechargeTable.ins().getInIdIndex(rechargeOrder.getProductId());
		if(rechargeCfg.getNotice() == 1) {
			marqueeService.push(1, rechargeOrder.getPlayerId(), rechargeOrder.getProductId());
		}
		// 累充邮件
		rechargePlayerService.onRecharge(rechargeOrder.getPlayerId(), rechargeOrder.getPayMoney());
	}

	@PlayerCmd
	public RechargeUnFinishResponse unFinish(PlayerContext playerContext, RechargeUnFinishRequest request) {
		RechargePlayer entity = rechargePlayerService.getEntity(playerContext.getId());

		RechargeUnFinishResponse.Builder response = RechargeUnFinishResponse.newBuilder();
		response.addAllRechargeIds(entity.getUnMarkOrders());
		return response.build();
	}

	@PlayerCmd
	public RechargeMarkFinishResponse markFinish(PlayerContext playerContext, RechargeMarkFinishRequest request) {
		RechargePlayer entity = rechargePlayerService.getEntity(playerContext.getId());
		
		int productId = 0;
		if(entity.getUnMarkOrders().contains(request.getRechargeId())) {
			entity.getUnMarkOrders().remove(request.getRechargeId());
			rechargePlayerService.update(entity);
			
			RechargeOrder order = rechargeOrderService.getEntity(request.getRechargeId());
			if(!order.isFinish()) {
				order.setFinish(true);
				rechargeOrderService.update(order);
				productId = order.getProductId();
			}
		}
		
		RechargeMarkFinishResponse.Builder response = RechargeMarkFinishResponse.newBuilder();
		response.setRechargeId(request.getRechargeId());
		response.setProductId(productId);
		return response.build();
	}
}
